//
//  putMyAnsViewController.m
//  Toyohu
//
//  Created by Heige on 16/5/9.
//  Copyright © 2016年 celia. All rights reserved.
//

#import "putMyAnsViewController.h"
#import <Masonry.h>
#import "TintView.h"
#import "PutQAViewController.h"
#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height

typedef void(^PushBlock)(void);

@interface putMyAnsViewController ()<UITextViewDelegate>
{
    UITextView *myTextView;
    UITextField*textField;
    UIButton *rightButton;
}

@property (nonatomic, copy) PushBlock pushBlock;
@property (nonatomic, strong) TintView *tintView;
@end

@implementation putMyAnsViewController

- (instancetype)initWithPush:(void (^)(void))pushBlock
{
    self = [super init];
    if (self) {
        self.pushBlock = pushBlock;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    
    [self CreateNavView];
    [self CreateContentView];
    
    
    
    
}

-(void)CreateNavView
{
    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH ,64)];
    navView.backgroundColor = [UIColor colorWithRed:253/255.0 green:214/255.0 blue:17/255.0 alpha:1];
    [self.view addSubview:navView];
    
    
    //导航栏左边项
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(20, 35, 10, 20);
    [leftButton setBackgroundImage:[UIImage imageNamed:@"ARROW---LEFT@2x"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(goBackPreviousPage) forControlEvents:UIControlEventTouchDown];
    
    [navView addSubview: leftButton];
    
    UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    navLabel.center = CGPointMake(navView.frame.size.width/2, navView.frame.size.height/2+10);
    navLabel.textColor = [UIColor whiteColor];
    navLabel.backgroundColor = [UIColor clearColor];
    navLabel.textAlignment = NSTextAlignmentCenter;
    navLabel.text = @"发布提问";
    navLabel.font = [UIFont systemFontOfSize:20];
    [navView addSubview:navLabel];
    
    rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [rightButton addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchDown];
    [rightButton setTitle:@"发布" forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [navView addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(navView.mas_trailing).offset(-20);
        make.width.equalTo(@(35));
        make.height.equalTo(@(40));
        make.centerY.equalTo(navLabel.mas_centerY);
    }];
}

-(void)CreateContentView
{
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 64, 60, 50)];
    titleLabel.text = @"标题";
    titleLabel.textColor = [UIColor lightGrayColor];
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.textAlignment = NSTextAlignmentCenter;
   // [self.view addSubview:titleLabel];
    
    textField = [[UITextField alloc]initWithFrame:CGRectMake(15, 64, WIDTH-20, 50)];
    textField.placeholder = @"标题";
    textField.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:textField];
    
    UIView *spaceView = [[UIView alloc]initWithFrame:CGRectMake(20, 114, WIDTH-40, 1)];
    spaceView.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    [self.view addSubview:spaceView];
    
    myTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 115, WIDTH-20, HEIGHT/2)];
    myTextView.backgroundColor= [UIColor whiteColor];
    myTextView.text = @"内容";
    myTextView.textColor = [UIColor grayColor];
    myTextView.delegate = self;
    myTextView.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:myTextView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelKeyBord)];
    [myTextView addGestureRecognizer:tap];
    
    
    
}
-(void)CreatetabBarView
{
    UIView * tabBarView = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT-50, WIDTH, 50)];
    tabBarView.backgroundColor = [UIColor colorWithRed:244/255.0 green:245/255.0 blue:237/255.0 alpha:1];
    [self.view addSubview:tabBarView];
    
    UIButton *cameraButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cameraButton.frame = CGRectMake(0, 0, 24, 15);
    cameraButton.center = CGPointMake(7*tabBarView.frame.size.width/8, tabBarView.frame.size.height/2);
    [cameraButton setBackgroundImage:[UIImage imageNamed:@"photo_camera_47.579390115668px_1200120_easyicon.net"] forState:UIControlStateNormal];
    [cameraButton addTarget:self action:@selector(doCamera) forControlEvents:UIControlEventTouchDown];
    [tabBarView addSubview:cameraButton];
    
    
    UIButton *pictureButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [pictureButton setBackgroundImage:[UIImage imageNamed:@"Picture_45.241379310345px_1198601_easyicon.net"] forState:UIControlStateNormal];
    [pictureButton addTarget:self action:@selector(getPicture) forControlEvents:UIControlEventTouchDown];
    [tabBarView addSubview:pictureButton];
    [pictureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(cameraButton.mas_left).offset(-40);
        make.centerY.equalTo(cameraButton.mas_centerY);
        make.width.equalTo(@(24));
        make.height.equalTo(@(15));
    }];
    
}

-(void)cancelKeyBord
{
    [myTextView resignFirstResponder];
}

-(void)goBackPreviousPage
{
     [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)sendMessage
{
    [rightButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    //弹框
    self.tintView = [[TintView alloc]initWithSuccessBlock:^{
        
//dismissViewControllerAnimated:completion:
       // [self dismissViewControllerAnimated:YES completion:nil];
       // [[NSNotificationCenter defaultCenter] //postNotificationName:@"pushQAViewC" object:self];
        
    }];
    
    [self.tintView.sureButton addTarget:self action:@selector(GoIntoPutQAView) forControlEvents:UIControlEventTouchDown];
    
    
    
    
}

-(void)GoIntoPutQAView
{
    [self dismissViewControllerAnimated:YES completion:nil];

//    PutQAViewController*vc = [[PutQAViewController alloc]init];
//    [self presentViewController:vc animated:YES completion:nil];
    self.pushBlock();
    [self.tintView removeFromSuperview];
}

-(void)doCamera
{
    
}

-(void)getPicture
{
    
}
#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length < 1){
        textView.text = @"内容";
        textView.textColor = [UIColor grayColor];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"内容"]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}


- (void)textViewDidChange:(UITextView *)textView {
    //只要前240个字
    if (textView.text.length > 200) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 200)];
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (textView.text.length == 200 && range.length == 0) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"发布内容超过200字了~" message:@"" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        return NO;
    }
    return YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
